//
//  CategoryView.swift
//  FM
//
//  Created by x on 2017/8/23.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol CategoryViewDelegate {
    
    /// 按钮点击代理
    ///
    /// - Parameter categotyID: 按钮的标记
    func categoryBtuClick(categotyID:String, btuTitle:String)
}
class CategoryView: UIView {

    var categoryDelegate:CategoryViewDelegate?
    
    var models:[CategoryModel]? {
        didSet {
            guard models != nil else {
                return
            }
            let W = bounds.width/4
            let h = bounds.height/2
            for i in 0..<models!.count {
                let model = models![i]
                let index = i/4
                let count = i%4
                let btuFrame = CGRect.init(x: (index == 0) ? CGFloat(i)*W :CGFloat(count)*W, y: (index == 0) ? 0 : h, width: W, height: h)
                
                //创建按钮
                let btu = CategoryBtu.creatButton(imageUrl: model.categoryImageURL!, frame: btuFrame, title: model.name!, tag: Int(model.id!)!, target: self, action: #selector(btnClick(btu:)))
                addSubview(btu)
            }
        }
    }
    
    /// 按钮的点击事件
    ///
    /// - Parameter btu: <#btu description#>
    func btnClick(btu: UIButton) -> Void {
        let model = models![btu.tag-1]
        categoryDelegate?.categoryBtuClick(categotyID: model.id!, btuTitle: model.name!)
    }
}
